/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 弹出对话框 支持comfirm dialog 
 * @author : Chen weihan <csq-3@163.com>
 * @since : 2014年08月05日 星期二 14时38分32秒
 * @filename : swift/mobile/js/componnet/dialog/zepto.dialog.js
 * @version : 
 * @description : 
 */

;(function(){

   var dialog = {

       open : function(opt) {

            switch(opt.type) {
               case 'comfirm':
                   this.comfirmDialog(opt);
               break;

               case 'contentDialog':
                   this.contentDialog(opt);
               break;

               case 'topTipDialog':
                   this.topTipDialog(opt);
               break;

               case 'payDialog' : 
                   this.close({dom:$('body')});
                   this.payDialog(opt);            
               break;

               case 'tipDialog' : 
                    this.tipDialog(opt);            
               break;

               case 'textDialog' : 
                    this.textDialog(opt);            
               break;


            }
       },


       close : function(opt) {
                   var dom = opt.dom;
                   dom.find('.pageDialog').remove();
                   dom.find('.bodyCover').remove();
                   dom.find('.textDialog').remove();
                   dom.find('.bodyCoverTransparent').remove();
       },
 
       positionDialog : function(dom,opt) {
             var sTop  = $(window).scrollTop(),
                 winH = $(window).height(),
                 domH = dom.height();
             if (opt.positionRelative) {
                 var pDomTop = opt.dom.offset().top,
                 top = sTop + (winH - domH - pDomTop)/2;
             } else {
                 var top = sTop + (winH - domH)/2;
             }

             dom.css({top : top+'px'});
       },

       backgroundTransparent : function(opt,type) {
             var html = "<div class='bodyCoverTransparent'></div>";
                 html = $(html);
                 html.on('touchstart touchmove touchend',function(e){
                    e.stopPropagation(); 
                    e.preventDefault(); 
                    return false;
                 });

            if (opt.dom.find('.bodyCoverTransparent').length < 1 && type) { 
               opt.dom.prepend(html);
            } else if (!type) {
               opt.dom.find('.bodyCoverTransparent').remove();         
            }
       },

 
       backgroundCover : function(opt,type) {
           var html = "<div class='bodyCover'></div>";
               html = $(html);
               html.on('touchstart touchmove touchend',function(e){
                    e.stopPropagation(); 
                    e.preventDefault(); 
               return false;
           });

           if (opt.positionRelative) {
               var pDomTop = opt.dom.offset().top,
               sTop  = $(window).scrollTop(),
               winH = $(window).height(),
               height =  winH - pDomTop;
               html.css({
                   top : sTop+'px',
                   height : height+'px'
               });

               if (opt.close) {
                  html.css({
                     'position':'fixed'
                  })
               }

               opt.dom.css({
                   '-webkit-transform': 'scale(1, 1)',
                   '-moz-transform': 'scale(1, 1)'
               })
           } 

           if (opt.dom.find('.bodyCover').length < 1 && type) { 
               opt.dom.prepend(html);
           } else if (!type) {
               opt.dom.find('.bodyCover').remove();         
           }
       },
 
       comfirmDialog : function(opt) {
           var dom = opt.dom,
               content = opt.content,
	           html =  "<div class='pageDialog'>"+
                       "     <div class='bgOpacity'></div>"+
                       "     <div class='comfirmTitle'></div>"+ 
                       "     <div class='comfirmContent'>"+content+"</div>"+ 
                       "     <div class='comfirmSubmit'>"+
                       "          <span class='comfirm'>YES</span> <span class='cancel'>NO</span>"+
                       "     </div>"+ 
                       "</div>";
               html = $(html);

               html.find('.comfirm').bind('tap',function(e){
                       html.remove(); 
                       opt.success == undefined ? '' : opt.success(e);               
               });

               html.find('.cancel').bind('tap',function(){
                       html.remove();
                       opt.cancel == undefined ? '' : opt.cancel(e);               
               });

           if (opt.cover) {
               this.backgroundCover(true);
           }

           if (dom.find('.comfirmDialog').length < 1 || opt.refresh) { 
               dom.append(html);
           }
       },

       payDialog : function(opt) {
           var tthis = this,dom = opt.dom,
           content = opt.content,
           html = "<div class='pageDialog'>"+
               "     <div class='payContent'>"+
               "<div class='diva'>Premium Service "; 
                             
           if (opt.close){ html = html+"<span class='payClose' > X </span>";}

           html = html+      "</div>"+
                             "<div class='divb'>Upgrade now to enjoy more privileges.</div>"+
                             "<div class='divc'><span>LEARN MORE </span> </div>"+
                             "     </div>"+ 
                             "</div>";

           html = $(html);
 
           html.on('touchstart touchmove touchend',function(e){
                  e.stopPropagation(); 
                  e.preventDefault(); 
           });

           html.find('.payClose').bind('touchend',function(e){
                  tthis.close(opt);  
                  });


           html.find('.divc').bind('touchend',function(e){
                   window.location.href='#upgrade/whole'; 
                   tthis.close(opt);  
           });

           /*
           html.find('.pageClose').bind('touchend',function(e){
                  html.remove(); 
                  tthis.backgroundCover(false);
                  });
            */
 
           if (opt.cover) {
               this.backgroundCover(opt,true);
           }

           if (dom.find('.pageDialog').length < 1 ) { 
               dom.prepend(html);
           }

           this.positionDialog(html,opt); 
       },

       tipDialog : function(opt) {
            var tthis = this,dom = opt.dom,
                content = opt.content,
                html =  "<div class='pageTip'><span>"+content+"</span></div>";
       
            html = $(html);

            if (dom.find('.pageTip').length < 1 ) { 
                dom.append(html);
            }

            var top = ($(window).height() - $(dom).offset().top - html.height())/2;

            html.css({top : top+'px'});
            
            setTimeout(function(){
                 dom.find('.pageTip').remove();
            },3000);
       },

       textDialog : function(opt) {
            var tthis = this,dom = opt.dom,
                content = opt.content,
                html =  "<div class='textDialog'><span>"+content+"</span></div>";
       
            html = $(html);

            if (dom.find('.textDialog').length < 1 ) { 
                dom.append(html);
            }
       },

       contentDialog : function(opt) {
            var tthis = this,dom = opt.dom,
                content = opt.content,
                html =  "<div class='pageDialog'>"+
                        "    <div class='payContent'>"+
                        "        <div class='diva'>Notice</div>"+
                        "        <div class='divb'>"+content+"</div>"+
                        "        <div class='divc'><span>Close</span> </div>"+
                        "    </div>"+ 
                        "</div>";
            html = $(html);

            html.on('touchstart touchmove touchend',function(e){
                e.stopPropagation(); 
                e.preventDefault(); 
            });

            html.find('.divc').bind('touchend',function(e){
                    html.remove(); 
                    tthis.backgroundTransparent(opt,false);
            });

            //if (opt.cover) {
                this.backgroundTransparent(opt,true);
            //}

            if (dom.find('.pageDialog').length < 1 ) { 
                dom.append(html);
            }

            this.positionDialog(html,opt);
        },


       topTipDialog : function(opt) {
            var tthis = this,dom = opt.dom,
                content = opt.content,
                html =  "<div id="+opt.rid+" class='topTipDialog'>"+
                        "    <div class='payContent'>"+
                        "        <span class='close'> X </span>"+
                        "        <table class='divb'><tr><td>"+content+"</td></tr></table>"+
                        "    </div>"+ 
                        "</div>";
            html = $(html);

            html.on('touchstart touchmove touchend',function(e){
                e.stopPropagation(); 
                e.preventDefault(); 
            });

            html.find('.close').bind('touchend',function(e){
                 close();
            });

            function close() {
                html.animate({
                   top:-(html.height())+'px'
                }, 350,function(){
                   html.remove(); 
                });
            }

            if (dom.find('#'+opt.rid).length < 1 ) { 
                dom.append(html);
                html.css({
                     'top':-html.height()+'px',
                     'background': (opt.state=='error') ? '#ffdee6' : '#2AC23B', 
                     'color': (opt.state=='error') ? '#f83265' :'#fff'
                }); 
                html.animate({
                   top:'0px'
                }, 350);
            }

            setTimeout(function(){
                 close();
            },2000);
        },

   }; 
   
   $.dialog = dialog;

   $(window).bind('orientationchange',function(){
        $('body').find('.pageDialog').each(function(){
             var sTop  = $(window).scrollTop(),
                 winH = $(window).height(),
                 dom = $(this).parent(),
                 domH = $(this).height(),
                 pDomTop = dom.offset().top,
                 top = sTop + (winH - domH - pDomTop)/2;
                 $(this).css({top : top+'px'});
        });
   });

   $(window).on('scroll',function(e){
        $('#'+seajs.moduleUI).find('.pageDialog').each(function(){
             var sTop  = $(window).scrollTop(),
                 winH = $(window).height(),
                 dom = $(this).parent(),
                 domH = $(this).height(),
                 pDomTop = dom.offset().top,
                 top = sTop + (winH - domH - pDomTop)/2;
             $(this).css({top : top+'px'});
        });
       
   });

   if (typeof define === "function" ) {
        define("dialog/zepto.dialog.js", [], function () { return $.dialog;} );
   }

})()

